<?php

namespace App\Http\Controllers\Admin;

use App\Http\Filters\DepartmentFilter;
use App\Models\Department;
use App\Http\Requests\DepartmentRequest;
use App\Http\Resources\DepartmentResource;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{
    public function index(DepartmentFilter $filter)
    {
        $departments = Department::query()
            ->filter($filter)
            ->paginate();

        return $this->ok(DepartmentResource::collection($departments));
    }

    public function create()
    {
        return $this->ok();
    }

    public function store(DepartmentRequest $request)
    {
        $inputs = $request->validated();
        $department = Department::create($inputs);

        return $this->created(DepartmentResource::make($department));
    }

    public function edit(Request $request, Department $department)
    {
        return $this->ok(DepartmentResource::make($department));
    }

    public function update(DepartmentRequest $request, Department $department)
    {
        $inputs = $request->validated();
        $department->update($inputs);

        return $this->created(DepartmentResource::make($department));
    }

    public function destroy(Department $department)
    {
        $department->delete();
        return $this->noContent();
    }
}
